package com.dstz.agilebpm.base.samples.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ApiIntercept implements HandlerInterceptor {

    private final static Logger logger = LoggerFactory.getLogger(ApiIntercept.class);

    /**
     * 业务处理器处理请求之前被调用，对用户的request进行处理，若返回值为true，则继续调用后续的拦截器和目标方法；若返回值为false，则终止请求；这里可以加上登录校验，权限拦截等。
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录处理
        //String token = request.getHeader("authorization");
        return false;
    }


    /**
     * Controller执行后但未返回视图前调用该方法，这里可以对返回用户前的模型数据进行加工处理。
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("当前postHandle方法");
    }

    /**
     * Controller执行后且返回视图后调用，可以得到Controller时的异常信息，这里可以记录操作日志，资源清理等
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("当前afterCompletion方法");
    }

}
